/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database_client;

import DataObject.TableObject;
import java.io.File;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Backkom
 */
public class TableValues extends AbstractTableModel {

    protected String[] tbHeader = null;
    protected File dataSourceFile;
    protected Vector<TableObject> dataSource;
    public boolean isEditCell = true;

    public TableValues(Vector<TableObject> dataRows, String[] tableHeader) {
        dataSource = dataRows;
        tbHeader = tableHeader;
    }

    public void addLine() {
        Object[] obj = new Object[tbHeader.length];
        Class[] classes = new Class[tbHeader.length];
        for (int i = 0; i < obj.length; i++) {
            obj[i] = null;
            classes[i] = String.class;
        }
        TableObject tbvl = new TableObject(obj.length);
        tbvl.setValues(obj, classes);
        dataSource.add(tbvl);
        System.out.println("added");
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return getDataSource().size();
    }

    @Override
    public int getColumnCount() {
        return tbHeader.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object value = getDataSource().elementAt(rowIndex).getAttributeAt(columnIndex);
        if (value == null) {
            value = "NULL";
        }
        return value;
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        dataSource.elementAt(row).setAttributeAt(column, value);
    }

    @Override
    public String getColumnName(int column) {
        if (column < tbHeader.length) {
            return tbHeader[column];
        }
        return null;
    }

    @Override
    public Class getColumnClass(int column) {
        Class dataType = super.getColumnClass(column);
        if (!getDataSource().isEmpty() && column < tbHeader.length) {
            dataType = dataSource.elementAt(0).getDataTypeAt(column);
        }
        return dataType;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return isEditCell;
    }

    /**
     * @return the dataSource
     */
    public Vector<TableObject> getDataSource() {
        return dataSource;
    }

    /**
     * @param dataSource the dataSource to set
     */
    public void setDataSource(Vector<TableObject> dataSource) {
        this.dataSource = dataSource;
    }
}
